<template>
	<view class="container">
		<view class="register-header">
			<text class="title">注册账号</text>
			<text class="subtitle">注册后享受更多服务</text>
		</view>
		
		<view class="register-form">
			<view class="form-item">
				<text class="label">手机号码</text>
				<view class="input-group">
					<input class="input" v-model="form.phone" placeholder="请输入手机号码" maxlength="11" type="number" />
					<view class="code-btn" @click="getVerifyCode" :class="{disabled: countdown > 0}">
						{{ countdown > 0 ? `${countdown}s后重新获取` : '获取验证码' }}
					</view>
				</view>
			</view>
			
			<view class="form-item">
				<text class="label">验证码</text>
				<input class="input" v-model="form.code" placeholder="请输入验证码" maxlength="6" type="number" />
			</view>
			
			<view class="form-item">
				<text class="label">设置密码</text>
				<input class="input" v-model="form.password" placeholder="请设置6-20位密码" type="password" maxlength="20" />
			</view>
			
			<view class="form-item">
				<text class="label">确认密码</text>
				<input class="input" v-model="form.confirmPassword" placeholder="请再次输入密码" type="password" maxlength="20" />
			</view>
			
			<view class="register-btn" @click="handleRegister">注册</view>
			
			<view class="actions">
				<text class="action-text">已有账号？</text>
				<text class="action-link" @click="goToLogin">立即登录</text>
			</view>
		</view>
		
		<view class="agreement">
			<text class="agreement-text">注册即表示同意</text>
			<text class="agreement-link">《用户协议》</text>
			<text class="agreement-text">和</text>
			<text class="agreement-link">《隐私政策》</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					phone: '',
					code: '',
					password: '',
					confirmPassword: ''
				},
				countdown: 0,
				timer: null
			}
		},
		onUnload() {
			// 清除定时器
			if (this.timer) {
				clearInterval(this.timer);
				this.timer = null;
			}
		},
		methods: {
			// 获取验证码
			getVerifyCode() {
				if (this.countdown > 0) {
					return;
				}
				
				// 表单验证
				if (!this.form.phone) {
					uni.showToast({
						title: '请输入手机号码',
						icon: 'none'
					});
					return;
				}
				
				if (!/^1\d{10}$/.test(this.form.phone)) {
					uni.showToast({
						title: '手机号码格式不正确',
						icon: 'none'
					});
					return;
				}
				
				// 显示加载
				uni.showLoading({
					title: '发送中...'
				});
				
				// 实际项目中应该调用发送验证码的接口
				// 这里模拟发送成功
				setTimeout(() => {
					uni.hideLoading();
					uni.showToast({
						title: '验证码已发送',
						icon: 'success'
					});
					
					// 开始倒计时
					this.countdown = 60;
					this.timer = setInterval(() => {
						this.countdown--;
						if (this.countdown <= 0) {
							clearInterval(this.timer);
							this.timer = null;
						}
					}, 1000);
				}, 1000);
			},
			
			// 注册
			handleRegister() {
				// 表单验证
				if (!this.form.phone) {
					uni.showToast({
						title: '请输入手机号码',
						icon: 'none'
					});
					return;
				}
				
				if (!/^1\d{10}$/.test(this.form.phone)) {
					uni.showToast({
						title: '手机号码格式不正确',
						icon: 'none'
					});
					return;
				}
				
				if (!this.form.code) {
					uni.showToast({
						title: '请输入验证码',
						icon: 'none'
					});
					return;
				}
				
				if (!this.form.password) {
					uni.showToast({
						title: '请设置密码',
						icon: 'none'
					});
					return;
				}
				
				if (this.form.password.length < 6) {
					uni.showToast({
						title: '密码长度不能少于6位',
						icon: 'none'
					});
					return;
				}
				
				if (this.form.password !== this.form.confirmPassword) {
					uni.showToast({
						title: '两次输入的密码不一致',
						icon: 'none'
					});
					return;
				}
				
				// 显示加载
				uni.showLoading({
					title: '注册中...'
				});
				
				// 调用注册接口
				this.$api.register({
					phone: this.form.phone,
					password: this.form.password,
					code: this.form.code
				})
				.then(res => {
					// 隐藏加载
					uni.hideLoading();
					
					// 提示
					uni.showToast({
						title: '注册成功',
						icon: 'success'
					});
					
					// 跳转到登录页
					setTimeout(() => {
						this.goToLogin();
					}, 1500);
				})
				.catch(err => {
					console.error('注册失败:', err);
					uni.hideLoading();
					uni.showToast({
						title: err.message || '注册失败，请稍后重试',
						icon: 'none'
					});
				});
			},
			
			// 跳转到登录页面
			goToLogin() {
				uni.navigateBack();
			}
		}
	}
</script>

<style lang="scss">
	.container {
		padding: 40rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
	}
	
	.register-header {
		margin: 60rpx 0;
		text-align: center;
	}
	
	.title {
		font-size: 40rpx;
		color: #333;
		font-weight: bold;
		margin-bottom: 20rpx;
		display: block;
	}
	
	.subtitle {
		font-size: 28rpx;
		color: #999;
	}
	
	.register-form {
		width: 100%;
		margin-bottom: 60rpx;
	}
	
	.form-item {
		margin-bottom: 30rpx;
	}
	
	.label {
		display: block;
		font-size: 28rpx;
		color: #333;
		margin-bottom: 15rpx;
	}
	
	.input {
		height: 90rpx;
		background-color: #f5f5f5;
		border-radius: 45rpx;
		padding: 0 40rpx;
		font-size: 28rpx;
	}
	
	.input-group {
		display: flex;
		position: relative;
	}
	
	.input-group .input {
		flex: 1;
		padding-right: 240rpx;
	}
	
	.code-btn {
		position: absolute;
		right: 10rpx;
		top: 10rpx;
		height: 70rpx;
		line-height: 70rpx;
		padding: 0 30rpx;
		background-color: #007aff;
		color: #fff;
		font-size: 26rpx;
		border-radius: 35rpx;
		z-index: 10;
	}
	
	.code-btn.disabled {
		background-color: #ccc;
	}
	
	.register-btn {
		height: 90rpx;
		line-height: 90rpx;
		background-color: #007aff;
		color: #fff;
		font-size: 32rpx;
		border-radius: 45rpx;
		text-align: center;
		margin-top: 60rpx;
	}
	
	.actions {
		display: flex;
		justify-content: center;
		margin-top: 30rpx;
	}
	
	.action-text {
		font-size: 28rpx;
		color: #666;
	}
	
	.action-link {
		font-size: 28rpx;
		color: #007aff;
	}
	
	.agreement {
		font-size: 24rpx;
		color: #999;
		margin-bottom: 30rpx;
		display: flex;
		flex-wrap: wrap;
		justify-content: center;
	}
	
	.agreement-link {
		color: #007aff;
		margin: 0 5rpx;
	}
</style>
